<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三角形内角和演示</title>
    <!-- 引入GeoGebra官方CDN -->
    <script src="https://www.geogebra.org/apps/deployggb.js"></script>
    <!-- 引入Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <!-- 引入Font Awesome图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        header {
            background-color: #f8f9fa;
            padding: 1rem;
            text-align: center;
        }
        .container {
            display: flex;
            flex: 1;
        }
        .geogebra-container {
            width: 600px;
            height: 500px;
            border: 1px solid #ccc;
        }
        .control-panel {
            padding: 1rem;
            width: 300px;
            background-color: #f8f9fa;
        }
        .slider {
            margin-bottom: 1rem;
        }
        .slider label {
            display: block;
            margin-bottom: 0.5rem;
        }
        .result {
            margin-top: 1rem;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <header>
        <h1>三角形内角和演示</h1>
    </header>
    <div class="container">
        <div id="geogebra-container" class="geogebra-container"></div>
        <div class="control-panel">
            <div id="sliders"></div>
            <div class="result" id="angle-sum">当前三角形内角和: 180°</div>
        </div>
    </div>
    <script>
        // GeoGebra初始化参数
        const parameters = {
            appName: "classic",
            width: "600",
            height: "500",
            showToolBar: true,
            showAlgebraInput: false,
            showMenuBar: false,
            allowStyleBar: true,
            enable3d: false,
            language: "zh-CN",
            appletOnLoad: function(api) {
                initializeSliders(api);
                updateTriangle(api);
            }
        };

        let applet;

        window.onload = function() {
            applet = new GGBApplet(parameters, true);
            applet.inject('geogebra-container');
        };

        function initializeSliders(api) {
            const slidersContainer = document.getElementById('sliders');
            const controls = [
                { type: "slider", label: "点A的x坐标", min: -10, max: 10, step: 0.1, defaultValue: 0 },
                { type: "slider", label: "点A的y坐标", min: -10, max: 10, step: 0.1, defaultValue: 0 },
                { type: "slider", label: "点B的x坐标", min: -10, max: 10, step: 0.1, defaultValue: 5 },
                { type: "slider", label: "点B的y坐标", min: -10, max: 10, step: 0.1, defaultValue: 0 },
                { type: "slider", label: "点C的x坐标", min: -10, max: 10, step: 0.1, defaultValue: 2.5 },
                { type: "slider", label: "点C的y坐标", min: -10, max: 10, step: 0.1, defaultValue: 4 }
            ];

            controls.forEach(control => {
                const sliderDiv = document.createElement('div');
                sliderDiv.classList.add('slider');
                const label = document.createElement('label');
                label.textContent = control.label;
                const input = document.createElement('input');
                input.type = 'range';
                input.min = control.min;
                input.max = control.max;
                input.step = control.step;
                input.value = control.defaultValue;
                input.addEventListener('input', () => updateTriangle(api));
                sliderDiv.appendChild(label);
                sliderDiv.appendChild(input);
                slidersContainer.appendChild(sliderDiv);
            });
        }

        function updateTriangle(api) {
            const points = [
                [document.querySelector('input[type="range"]:nth-child(1)').value, document.querySelector('input[type="range"]:nth-child(2)').value],
                [document.querySelector('input[type="range"]:nth-child(3)').value, document.querySelector('input[type="range"]:nth-child(4)').value],
                [document.querySelector('input[type="range"]:nth-child(5)').value, document.querySelector('input[type="range"]:nth-child(6)').value]
            ];

            api.evalCommand(`A = (${points[0][0]}, ${points[0][1]})`);
            api.evalCommand(`B = (${points[1][0]}, ${points[1][1]})`);
            api.evalCommand(`C = (${points[2][0]}, ${points[2][1]})`);
            api.evalCommand('Poly1 = Polygon(A, B, C)');
            api.evalCommand('AngleA = Angle(B, A, C)');
            api.evalCommand('AngleB = Angle(C, B, A)');
            api.evalCommand('AngleC = Angle(A, C, B)');
            const angleSum = parseFloat(api.getValueString('AngleA')) + parseFloat(api.getValueString('AngleB')) + parseFloat(api.getValueString('AngleC'));
            document.getElementById('angle-sum').textContent = `当前三角形内角和: ${angleSum.toFixed(2)}°`;
        }
    </script>
</body>
</html>